package com.yida.admin;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yida.bean.blogger;
import com.yida.bean.blogtype;
import com.yida.service.serverblog;

@Controller
@RequestMapping("admin")
public class types {

	@Autowired
	public serverblog service;
	
	@RequestMapping("/types")
	public String types(Model mode,Integer page) {
		 Integer pages = 1;
			if (page!=null) {
				pages=page;
			}
		Integer   pageall   = service.selectTypeSumservice();
		List<blogtype> typelist = service.selectBlogtypesservice(pages);
		
	    mode.addAttribute("typelist", typelist);
		mode.addAttribute("pages", pageall);
		mode.addAttribute("cpage", pages);
		
		return "admin/types";
	}
	
	
	@RequestMapping("/types/delete")
	public String typesdelete(Model mode,Integer id,RedirectAttributes attributes) {
			if (id!=null) {
		                  Integer jie	= service.deletetypeservice(id);	                
		             if (jie>0) {
		            	 attributes.addFlashAttribute("message","删除成功");	               
					}else{
						 attributes.addFlashAttribute("message","无该条数据");
		                 
					}         
			}
			else {
				 attributes.addFlashAttribute("message", "无效操作");   
			}     
			 
		 return "redirect:/admin/types";
	}
	
	@RequestMapping("/types/updateinput")
	public String typesupdateinput(Model mode,Integer id,String type) {	
		 mode.addAttribute("id", id);
		 mode.addAttribute("type", type);   
		 return "admin/types-input"; 
	}
	
	@RequestMapping("/types/update")
	public String typesupdate(Model mode,blogtype type,RedirectAttributes attributes) {
		
		Integer jie = service.updatetypeservice(type);
		   if (jie>0) {
          	 attributes.addFlashAttribute("message","编辑成功!");
        	 return "redirect:/admin/types";
			}else{
			 attributes.addFlashAttribute("message","编辑失败!");
			 return "redirect:/admin/types";
				  
			}       
	}
	
	@RequestMapping("/types/insertinput")
	public String typesupdateinput(Model mode) {	
		 return "admin/types-input"; 
	}
	
	@RequestMapping("/types/insert")
	public String typesinsert(Model mode,blogtype type,RedirectAttributes attributes) {
		
		Integer jie = service.inserttypeservice(type);
		   if (jie>0) {
          	 attributes.addFlashAttribute("message","新增成功");
        	 return "redirect:/admin/types";
			}else{	
				  return "redirect:/admin/types/insertinput";
			}       
	}
	
	
	
	
	
}

